package com.ls.lserp.modules.produce.service;





import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.modules.order.entity.BigcargoOrder;
import com.ls.lserp.modules.order.entity.ChangeMaterial;
import com.ls.lserp.modules.order.entity.OrderMateriel;
import com.ls.lserp.modules.order.entity.SizeColorQuantity;
import com.ls.lserp.modules.order.entity.StyleFeedBack;
import com.ls.lserp.modules.produce.dao.ProduceLicenseDao;
import com.ls.lserp.modules.produce.dao.ProduceNoticeDao;
import com.ls.lserp.modules.produce.entity.ProduceLicense;
import com.ls.lserp.modules.produce.entity.ProduceNotice;
import com.ls.lserp.modules.produce.entity.ProducePlan;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.SequenceUtil;
@Service
@Transactional(readOnly = false)
public class ProduceLicenseService extends CrudService<ProduceLicenseDao,ProduceLicense>{

	
	
	@Autowired
    private ProduceLicenseDao produceLicenseDao;
	
	@Autowired
    private ProduceNoticeDao produceNoticeDao;
	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    /*public Page<ProduceLicense> findProduceNoticeList(Page<ProduceLicense> page, ProduceLicense produceLicense) {
	        // 生成数据权限过滤条件（dsf为dataScopeFilter的简写，在xml中使用 ${sqlMap.dsf}调用权限SQL）
	        //designOption.getSqlMap().put("dsf", dataScopeFilter(user.getCurrentUser(), "o", "a"));
	        // 设置分页参数
	    	produceLicense.setPage(page);
	        // 执行分页查询
	        page.setList(produceLicenseDao.findList(produceLicense));
	        return page;
	    }*/
	    
	    /**
		 * 查询父项
		 * @param page
		 * @param user
		 * @return
		 */
	    public List<ProduceLicense> findProduceNoticeList(ProduceLicense produceLicense) {
	        return produceLicenseDao.findList(produceLicense);
	    }
	    /**
		 * 查询一条生产制单信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<ProduceLicense> findGroupList(){
	    	
	    	return produceLicenseDao.findGroupList();
	    }
	    
		/**
		 * 查询一条生产制单信息
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceLicense getLicense(ProduceLicense produceLicense) {
	    	produceLicense = produceLicenseDao.get(produceLicense);
	    	List<ProduceLicense> indentList = produceLicenseDao.findIndentNOList(produceLicense.getOgnizationID());
			List<ProduceLicense> customerList = produceLicenseDao.findCustomerList();
			List<ProduceLicense> groupList = produceLicenseDao.findGroupList();
			produceLicense.setIndentList(indentList);
			produceLicense.setCustomerList(customerList);
			produceLicense.setGroupList(groupList);
	        return produceLicense;
	        
	    }
	    
	    /**
		 * 通过生产制单查询生产通知单信息
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceNotice getNotice(ProduceNotice produceNotice) {
	    	produceNotice = produceNoticeDao.get(produceNotice);
	    	OrderMateriel orderMateriel = new OrderMateriel();
	    	if ("1".equals(produceNotice.getNoticeFlag())) {
	    		orderMateriel = produceNoticeDao.getContractInfo(produceNotice.getContractID());
			}else {
				orderMateriel = produceNoticeDao.getOrderInfo(produceNotice.getIndentID());
			}
	    	produceNotice.setOrderMateriel(orderMateriel);
	        return produceNotice;
	        
	    }
	    
	    /**
		 * ID查询一条生产制单信息
		 * 
		 * @param user
		 * @return
		 */
	    public ProduceLicense findByID(Long id){
	    	
	    	return produceLicenseDao.get(id);
	    }
	    /**
		 * 获取订单集
		 * 
		 * @param user
		 * @return
		 */
	    public List<ProduceLicense> findIndentNOList(Long id){
	    	
	    	return produceLicenseDao.findIndentNOList(id);
	    }
	    
	    
	    /**
		 * 新增/修改一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void saveProduceLicense(ProduceLicense produceLicense) {
	    	// 判断新建编辑
	    		//若为null 执行插入语句（新建）
	    		if (produceLicense.getId()==null){
	    			// 保存
	    			if (!"true".equals(produceLicense.getSourceNotice())) {
	    				StringBuilder conCode = new StringBuilder();
	    				conCode.append("L");
	    				conCode.append(DateUtils.formatDate(new Date(), "yyyyMMdd"));
	    				Sequence sequence=SequenceUtil.ognizationCode(conCode.toString(), "sequence_produceLicense", 3);
	    				produceLicense.setProductionNO(sequence.getReturnCode());
					}
    		        produceLicense.preInsert();
    		        produceLicense.setMaterialStatus("0");
    		        produceLicense.setProStatus("0");
    		        produceLicenseDao.insert(produceLicense);
    		        //produceLicenseDao.insertPlanNotice(produceLicense);
    		        if ("true".equals(produceLicense.getSourceNotice())) {
    		        	//插入通知单制单关联表
    		        	ProduceNotice produceNotice = new ProduceNotice();
    		        	produceNotice.preInsert();
    		        	produceNotice.setProductionId(produceLicense.getId());
    		        	produceNotice.setId(produceLicense.getNoticeID());
    		        	produceNoticeDao.insertNoticeLicense(produceNotice);
    		        	//更新通知单状态
    		        	produceNotice.setMaterialStatus(2);
    		        	produceNoticeDao.updateStatus(produceNotice);
					}
	    				
	    		}else{
	    			// 更新数据
	    			produceLicense.preInsert();
	    			if (produceLicense.getProFlag()==0) {
	    				produceLicenseDao.update(produceLicense);
					}else {
						produceLicenseDao.updateCus(produceLicense);
					}
	    		}			
	    }
	    
	    /**
		 * 查询客户数据
		 * 
		 * @param user
		 * @return
		 */
	    public List<ProduceLicense> findCustomerList(){
	    	
	    	return produceLicenseDao.findCustomerList();
	    	
	    }
	    /**
		 * 获取一条订单相关信息
		 * 
		 * @param user
		 * @return
		 */
		public BigcargoOrder getIndentInfo(ProduceLicense produceLicense){
	    	
	    	return produceLicenseDao.getIndentInfo(produceLicense);
	    	
	    }
	    /**
		 * 查询颜色尺码信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<SizeColorQuantity> findSizeColor(ProduceLicense produceLicense){
	    	
	    	return produceLicenseDao.findSizeColor(produceLicense);
	    	
	    }
	    /**
		 * 查询面辅料信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<ChangeMaterial> findMaterial(ProduceLicense produceLicense){
	    	
	    	return produceLicenseDao.findMaterial(produceLicense);
	    	
	    }
	    /**
		 * 查询意见信息
		 * 
		 * @param user
		 * @return
		 */
	    public List<StyleFeedBack> findFeedBack(ProduceLicense produceLicense){
	    	
	    	return produceLicenseDao.findFeedBack(produceLicense);
	    	
	    }
	    
	    
	    
	    /**
		 * 提交一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void updateStatus(ProduceNotice produceNotice) {
	    			// 更新数据
	    	produceNotice.preInsert();
	    	produceNotice.setMaterialStatus(1);
	    	produceLicenseDao.updateStatus(produceNotice);
	    }
	    /**
		 * 删除一条数据
		 * @param user
		 */
	    @Transactional(readOnly = false)
	    public void deleteProducePlan(ProduceLicense produceLicense) {
	    	// 更新数据
	    	produceLicense.preInsert();
	    	produceLicense.setDeleteFlag("1");
	    		produceLicenseDao.delete(produceLicense);
	    }
	    
}
